<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>管理员管理界面</title>
<link rel="stylesheet"
	href="https://fonts.googleapis.com/css?family=Roboto:400,700" />
<!-- https://fonts.google.com/specimen/Roboto -->
<link rel="stylesheet" href="../css/fontawesome.min.css" />
<!-- https://fontawesome.com/ -->
<link rel="stylesheet" href="../css/bootstrap.min.css" />
<!-- https://getbootstrap.com/ -->
<link rel="stylesheet" href="../css/templatemo-style.css">
<!--
	Product Admin CSS Template
	https://templatemo.com/tm-524-product-admin
	-->
</head>
<script src="../js/jquery-1.7.2.js"></script>
<script type="text/javascript">
	let re = new RegExp("\s?" + "username" + "=([^;]+)(;|$)")
	var username = document.cookie.match(re)[1];
	var arry;
	var arry2;
	function manageItem() {
		$.ajax({
					url : "http://localhost:8080/voting-system/getAllItem",
					type : "post",
					success : function(data) {
						arry = data;
						var tbhtml = "<table class=\"table table-hover tm-table-small tm-product-table\"><thead><tr><th scope=\"col\">&nbsp;</th><th scope=\"col\">序号</th><th scope=\"col\">投票标题</th></tr></thead><tbody>";
						for (var i = 0; i < data.length; i++) {
							var j = i + 1;
							tbhtml += "<tr><th scope=\"row\"><input type=\"radio\" name=\"option\" value=\""+data[i].item_id+"\"/></th><td class=\"tm-product-name\">"+j+"</td><td>"+data[i].item_title+"</td></tr>";
						}
						tbhtml += "</tbody></table>";
						$("#voteItem").html(tbhtml);
						$.ajax({
				            url: "http://localhost:8080/voting-system/getUserId",
				            type: "post",
				            success:function (data) {
				            	arry2=data;
				            	user_id=arry2["UserId"];
				            	$.ajax({
				                    url: "http://localhost:8080/voting-system/getUser",
				                    type: "post",
				                    success:function (data) {
				                    	$("#name").html(data[0].username);
				                    	$("#sex").html(data[0].sex);
				                    	$("#age").html(data[0].age);
				                    	$("#tel").html(data[0].telephone);
				                    },
				                    dataType: "json",
				                    data:{user_id:user_id}
				                });
				            },
				            dataType: "json",
				            data:{username:username}
				        });
					},
					dataType : "json"
				});

	}
	function addItem(){
		window.location.href = "add-product.html";
	}
	function updateItem(){
		var value="";
		var radio=document.getElementsByName("option");
		for(var i=0;i<radio.length;i++){
			if(radio[i].checked==true){
				value=radio[i].value;
				break;
			}
		}
		document.cookie = 'item_id='+value+';'
		if(value==""){
			alert("还未选择项目！")
		}else{
			window.location.href = "edit-product.html";
		}
	}
	function deleteItem(){
		var value="";
		var radio=document.getElementsByName("option");
		for(var i=0;i<radio.length;i++){
			if(radio[i].checked==true){
				value=radio[i].value;
				break;
			}
		}
		$.ajax({
			url : "http://localhost:8080/voting-system/deleteItem",
			type : "post",
			success : function(data) {
				if(data.delectItem==false){
					alert("删除失败！")
				}else{
					alert("删除成功！")
					window.location.href='manage.html'
				}
			},
			dataType : "json",
			data:{item_id:value}
		});
	}
</script>
<body id="reportsPage" onload="manageItem()">
	<nav class="navbar navbar-expand-xl">
		<div class="container h-100">
			<h1 class="tm-site-title mb-0">投票管理</h1>
			<button class="navbar-toggler ml-auto mr-0" type="button"
				data-toggle="collapse" data-target="#navbarSupportedContent"
				aria-controls="navbarSupportedContent" aria-expanded="false"
				aria-label="Toggle navigation">
				<i class="fas fa-bars tm-nav-icon"></i>
			</button>

			<div class="collapse navbar-collapse" id="navbarSupportedContent">
				<ul class="navbar-nav mx-auto h-100">
					<li class="nav-item"><a class="nav-link" href="result.jsp">
							<i class="fas fa-tachometer-alt"></i> 投票结果 <span class="sr-only">(current)</span>
					</a></li>
					<li class="nav-item dropdown"><a
						class="nav-link dropdown-toggle" href="update.html"> <i
							class="far fa-file-alt"></i> <span>用户管理 </span>
					</a></li>
					<li class="nav-item"><a class="nav-link active"
						href="manage.html"> <i class="fas fa-shopping-cart"></i> 投票管理
					</a></li>

					<li class="nav-item"><a class="nav-link" href="center.html">
							<i class="far fa-user"></i> 个人中心
					</a></li>
				</ul>
				<ul class="navbar-nav">
					<li class="nav-item"><a class="nav-link d-block"
						href="../login.html"> <b>退出登录</b>
					</a></li>
				</ul>
			</div>
		</div>
	</nav>
	<form method="post">
		<div class="container mt-5">
			<div class="row tm-content-row">
				<div class="col-sm-12 col-md-12 col-lg-8 col-xl-8 tm-block-col">
					<div class="tm-bg-primary-dark tm-block tm-block-products">
						<h2 class="tm-block-title">搜索项目列表</h2>
						<div class="tm-product-table-container" id="voteItem">
							<!--  <table class="table table-hover tm-table-small tm-product-table">
							<thead>
								<tr>
									<th scope="col">&nbsp;</th>
									<th scope="col">序号</th>
									<th scope="col">投票内容</th>
									<th scope="col">投票总数</th>
									<th scope="col">投票时间</th>
									<th scope="col">&nbsp;</th>
								</tr>
							</thead>
							<tbody>
								<tr>
									<th scope="row"><input type="checkbox" /></th>
									<td class="tm-product-name">1</td>
									<td>1,450</td>
									<td>550</td>
									<td>28 March 2019</td>
									<td><a href="#" class="tm-product-delete-link"> <i
											class="far fa-trash-alt tm-product-delete-icon"></i>
									</a></td>
								</tr>
								<tr>
									<th scope="row"><input type="checkbox" /></th>
									<td class="tm-product-name">2</td>
									<td>1,250</td>
									<td>750</td>
									<td>21 March 2019</td>
									<td><a href="#" class="tm-product-delete-link"> <i
											class="far fa-trash-alt tm-product-delete-icon"></i>
									</a></td>
								</tr>
								<tr>
									<th scope="row"><input type="checkbox" /></th>
									<td class="tm-product-name">3</td>
									<td>1,100</td>
									<td>900</td>
									<td>18 Feb 2019</td>
									<td><a href="#" class="tm-product-delete-link"> <i
											class="far fa-trash-alt tm-product-delete-icon"></i>
									</a></td>
								</tr>
								<tr>
									<th scope="row"><input type="checkbox" /></th>
									<td class="tm-product-name">4</td>
									<td>1,400</td>
									<td>600</td>
									<td>24 Feb 2019</td>
									<td><a href="#" class="tm-product-delete-link"> <i
											class="far fa-trash-alt tm-product-delete-icon"></i>
									</a></td>
								</tr>
								<tr>
									<th scope="row"><input type="checkbox" /></th>
									<td class="tm-product-name">5</td>
									<td>1,800</td>
									<td>200</td>
									<td>22 Feb 2019</td>
									<td><a href="#" class="tm-product-delete-link"> <i
											class="far fa-trash-alt tm-product-delete-icon"></i>
									</a></td>
								</tr>
								<tr>
									<th scope="row"><input type="checkbox" /></th>
									<td class="tm-product-name">6</td>
									<td>1,000</td>
									<td>1,000</td>
									<td>20 Feb 2019</td>
									<td><a href="#" class="tm-product-delete-link"> <i
											class="far fa-trash-alt tm-product-delete-icon"></i>
									</a></td>
								</tr>
								<tr>
									<th scope="row"><input type="checkbox" /></th>
									<td class="tm-product-name">7</td>
									<td>500</td>
									<td>100</td>
									<td>10 Feb 2019</td>
									<td><a href="#" class="tm-product-delete-link"> <i
											class="far fa-trash-alt tm-product-delete-icon"></i>
									</a></td>
								</tr>
								<tr>
									<th scope="row"><input type="checkbox" /></th>
									<td class="tm-product-name">8</td>
									<td>1,000</td>
									<td>600</td>
									<td>08 Feb 2019</td>
									<td><a href="#" class="tm-product-delete-link"> <i
											class="far fa-trash-alt tm-product-delete-icon"></i>
									</a></td>
								</tr>
								<tr>
									<th scope="row"><input type="checkbox" /></th>
									<td class="tm-product-name">9</td>
									<td>1,200</td>
									<td>800</td>
									<td>24 Jan 2019</td>
									<td><a href="#" class="tm-product-delete-link"> <i
											class="far fa-trash-alt tm-product-delete-icon"></i>
									</a></td>
								</tr>
								<tr>
									<th scope="row"><input type="checkbox" /></th>
									<td class="tm-product-name">10</td>
									<td>1,600</td>
									<td>400</td>
									<td>22 Jan 2019</td>
									<td><a href="#" class="tm-product-delete-link"> <i
											class="far fa-trash-alt tm-product-delete-icon"></i>
									</a></td>
								</tr>
								<tr>
									<th scope="row"><input type="checkbox" /></th>
									<td class="tm-product-name">11</td>
									<td>2,000</td>
									<td>400</td>
									<td>21 Jan 2019</td>
									<td><a href="#" class="tm-product-delete-link"> <i
											class="far fa-trash-alt tm-product-delete-icon"></i>
									</a></td>
								</tr>
							</tbody>
						</table>-->
						</div>
						
						<button type="button"
							class="btn btn-primary btn-block text-uppercase mb-3" onclick="addItem()">添加投票项目</button>
						<button type="button" class="btn btn-primary btn-block text-uppercase" onclick="updateItem()">
							修改所选项目</button>
						<button type="button" class="btn btn-primary btn-block text-uppercase" onclick="deleteItem()">
							删除所选项目</button>

					</div>
				</div>
	</form>

	<div class="col-sm-12 col-md-12 col-lg-4 col-xl-4 tm-block-col">
		<div class="tm-bg-primary-dark tm-block tm-block-product-categories">
		<h2 align="center" class="tm-block-title">管理员</h2>
			<div class="form-group col-lg-6">
							<label >用户名</label> <label name="name" id="name" style="width: 200px;"
								class="form-control validate"></label>
						</div>

						<div class="form-group col-lg-6">
							<label >性别</label> <label name="sex" id="sex" style="width: 200px;"
								class="form-control validate"></label>
						</div>
						<div class="form-group col-lg-6">
							<label >年龄</label> <label name="age" id="age" style="width: 200px;"
								class="form-control validate"></label>
						</div>
						<div class="form-group col-lg-6">
							<label >电话</label> <label name="tel" id="tel" style="width: 200px;"
								class="form-control validate"></label>
						</div>
		</div>
	<!--  <div class="col-sm-12 col-md-12 col-lg-4 col-xl-4 tm-block-col">
		<div class="tm-bg-primary-dark tm-block tm-block-product-categories">
			<h2 class="tm-block-title">搜索投票项目</h2>
			<form method="post">
				<input name="word" style="border-radius: 15px;" type="text"
					placeholder="搜索投票名称" class="form-control validate"> <br>
				<input name="id" style="border-radius: 15px;" type="text"
					placeholder="搜索投票序号" class="form-control validate"> <br>

				<button class="btn btn-primary btn-block text-uppercase mb-3">
					搜索</button>
			</form>
		</div>

	</div>-->


	<!--  <script src="../js/jquery-3.3.1.min.js"></script>
	<script src="../js/bootstrap.min.js"></script>
	<script>
		$(function() {
			$(".tm-product-name").on("click", function() {
				window.location.href = "edit-product.html";
			});
		});
	</script>*/-->
</body>

</html>